﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MipsPipelineSimulator
{
    public partial class AddressSetting : Form
    {
        private MainForm mainForm = null;

        private AddressSetting()
        {
            InitializeComponent();
        }

        public AddressSetting(MainForm parent)
        {
            this.mainForm = parent;
            InitializeComponent();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            switch (typeComboBox.SelectedIndex)
            {
                case 0:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.SByte, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.SByte, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.SByte);
                            string strValue = CommonTool.NumToStr(TypeCode.SByte, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 1:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Byte, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Byte, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Byte);
                            string strValue = CommonTool.NumToStr(TypeCode.Byte, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 2:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Int16, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Int16, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Int16);
                            string strValue = CommonTool.NumToStr(TypeCode.Int16, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 3:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.UInt16, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.UInt16, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.UInt16);
                            string strValue = CommonTool.NumToStr(TypeCode.UInt16, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 4:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Int32, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Int32, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Int32);
                            string strValue = CommonTool.NumToStr(TypeCode.Int32, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 5:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.UInt32, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.UInt32, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.UInt32);
                            string strValue = CommonTool.NumToStr(TypeCode.UInt32, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 6:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Int64, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Int64, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Int64);
                            string strValue = CommonTool.NumToStr(TypeCode.Int64, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 7:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.UInt64, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.UInt64, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.UInt64);
                            string strValue = CommonTool.NumToStr(TypeCode.UInt64, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 8:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Single, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Single, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Single);
                            string strValue = CommonTool.NumToStr(TypeCode.Single, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
                case 9:
                    {
                        if (addressTextBox.Text == null || addressTextBox.Text.Trim() == "")
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值不可以为空!");
                            msgBox.ShowDialog(this); return;
                        }
                        object address = CommonTool.StrToNum(TypeCode.Int64, addressTextBox.Text, 10);
                        if (address == null)
                        {
                            DefinedMsgBox msgBox = new DefinedMsgBox("错误", "地址值格式错误!");
                            msgBox.ShowDialog(this); return;
                        }

                        if (setRadioButton.Checked)
                        {
                            if (valueTextBox.Text == null || valueTextBox.Text.Trim() == "")
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值不可以为空!");
                                msgBox.ShowDialog(this); return;
                            }


                            object num = CommonTool.StrToNum(TypeCode.Double, valueTextBox.Text, 10);
                            if (num == null)
                            {
                                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "内存值格式错误!");
                                msgBox.ShowDialog(this); return;
                            }
                            mainForm.commandForm.memoryControl.SetMemoryValue((long)address, TypeCode.Double, num);
                        }
                        else
                        {
                            object result = mainForm.commandForm.memoryControl.GetMemoryValue((long)address, TypeCode.Double);
                            string strValue = CommonTool.NumToStr(TypeCode.Double, result, "D", false);
                            valueTextBox.Text = strValue;
                        }
                        break;
                    }
            }
        }

        private void AddressSetting_Load(object sender, EventArgs e)
        {
            typeComboBox.SelectedIndex = 4;
        }
    }
}
